Page({

  /**
   * 页面的初始数据
   */
  data: {
    truename: '',
    username: '',
    password: '',
    sex: '',
    age: '',
    tel: '',
    desc: '',
    docid: '',

    get_truename: '',
    get_username: '',
    get_password: '',
    get_sex: '',
    get_age: '',
    get_tel: '',
    get_desc: '',
  },

  // 从缓存读取当前修改的工人的信息
  getworkerdetail: function () {
    wx.getStorage({
      key: 'edit_worker_detail',
    }).then(res => {
      this.setData({
        truename: res.data.name,
        username: res.data.username,
        password: res.data.password,
        sex: res.data.sex,
        age: res.data.age,
        tel: res.data.tel,
        desc: res.data.desc,
        docid: res.data._id
      })
    })
  },

  // 获取各项输入的数据并存入data中
  get_truename: function (e) {
    // console.log(e.detail.value);
    this.setData({
      get_truename: e.detail.value
    })
  },
  get_username: function (e) {
    // console.log(e.detail.value);
    this.setData({
      get_username: e.detail.value
    })
  },
  get_password: function (e) {
    // console.log(e.detail.value);
    this.setData({
      get_password: e.detail.value
    })
  },
  get_sex: function (e) {
    // console.log(e.detail.value);
    this.setData({
      get_sex: e.detail.value
    })
  },
  get_age: function (e) {
    // console.log(e.detail.value);
    this.setData({
      get_age: e.detail.value
    })
  },
  get_tel: function (e) {
    // console.log(e.detail.value);
    this.setData({
      get_tel: e.detail.value
    })
  },
  get_desc: function (e) {
    // console.log(e.detail.value);
    this.setData({
      get_desc: e.detail.value
    })
  },


  // 点击立即提交来提交数据
  upload_workerdetail: function () {
    if (this.data.get_truename == '') {
      this.data.get_truename = this.data.truename;
    }  if (this.data.get_username == '') {
      this.data.get_username = this.data.username;
    }  if (this.data.get_password == '') {
      this.data.get_password = this.data.password;
    }  if (this.data.get_sex == '') {
      this.data.get_sex = this.data.sex;
    }  if (this.data.get_age == '') {
      this.data.get_age = this.data.age;
    }  if (this.data.get_tel == '') {
      this.data.get_tel = this.data.tel;
    }  if (this.data.get_desc == '') {
      this.data.get_desc = this.data.desc;
    }
    wx.showModal({
      title: '提示',
      content: '是否确认提交？'
    }).then(res => {
      wx.showLoading({
        title: '加载中',
      })
      if (res.confirm) {
        wx.cloud.callFunction({
          name: 'update',
          data: {
            options: 'editworker',
            truename: this.data.get_truename,
            username: this.data.get_username,
            password: this.data.get_password,
            sex: this.data.get_sex,
            age: this.data.get_age,
            tel: this.data.get_tel,
            desc: this.data.get_desc,
            docid: this.data.docid,
          }
        }).then(res => {
          console.log(res);
          wx.hideLoading().then(res=>{
            wx.navigateTo({
              url: '../../pages/admin_workers/admin_workers',
            })
            wx.showToast({
              title: '修改成功',
              mask:true,
              duration:2000
            })
          })
        })
      } else if (res.cancel) {
        console.log("用户点击取消");
        wx.hideLoading();
      }
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getworkerdetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.removeStorageSync('edit_worker_detail')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})